home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 343_02 / hist.c < prev    next >
C/C++ Source or Header  |  1992-02-27  |  12KB  |  504 lines

  1.  
  2.  
  3.  
  4.        /*****************************************************
  5.        *
  6.        *       file d:\cips\hist.c
  7.        *
  8.        *       Functions: This file contains
  9.        *           calculate_histogram
  10.        *           calculate_histogram
  11.        *           zero_histogram
  12.        *           perform_histogram_equalization
  13.        *           show_histogram
  14.        *           print_histogram
  15.        *           smooth_histogram
  16.        *
  17.        *
  18.        *       Purpose: These functions calculate and display the
  19.        *          histogram of an input image array.
  20.        *
  21.        *       Modifications:
  22.        *           July 86 - ported to IBM-PC
  23.        *           August 1990 - modified for use in the
  24.        *               C Image Processing System
  25.        *           March 1992 - removed the hardwired values
  26.        *               of 100 and replaced them with ROWS
  27.        *               and COLS.  There are still some
  28.        *               hardwired numbers in this file, but
  29.        *               they deal with displaying a histogram.
  30.        *
  31.        *******************************************************/
  32.  
  33. #include "d:\cips\cips.h"
  34.  
  35.  
  36. #define PRINT_WIDTH  80
  37. #define FORMFEED     '\014'
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.         /*****************************************
  48.         *
  49.         *    perform_histogram_equalization(...
  50.         *
  51.         ******************************************/
  52.  
  53. perform_histogram_equalization(image, histogram, new_grays, area)
  54.    float new_grays, area;
  55.    short image[ROWS][COLS];
  56.    unsigned long histogram[];
  57. {
  58.    int i,
  59.        j,
  60.        k;
  61.    unsigned long sum,
  62.             sum_of_h[256];
  63.  
  64.    double constant;
  65.  
  66.  
  67.  
  68.    sum = 0;
  69.    for(i=0; i<256; i++){
  70.       sum         = sum + histogram[i];
  71.       sum_of_h[i] = sum;
  72.    }
  73.  
  74.       /* constant = new # of gray levels div by area */
  75.    constant = new_grays/area;
  76.    for(i=0; i<ROWS; i++){
  77.       for(j=0; j<COLS; j++){
  78.          k           = image[i][j];
  79.          image[i][j] = sum_of_h[k] * constant;
  80.       }
  81.    }
  82. }  /* ends perform_histogram_equalization */
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.         /*****************************************
  95.         *
  96.         *   zero_histogram(...
  97.         *
  98.         ******************************************/
  99.  
  100. zero_histogram(histogram)
  101.    unsigned long histogram[];
  102. {
  103.    int i;
  104.    for(i=0; i<256; i++)
  105.       histogram[i] = 0;
  106. }  /* ends zero_histogram */
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.         /*****************************************
  116.         *
  117.         *   calcualte_histogram(...
  118.         *
  119.         ******************************************/
  120.  
  121. calculate_histogram(image, histogram)
  122.    short  image[ROWS][COLS];
  123.    unsigned long histogram[];
  124. {
  125.    int i,j,k;
  126.    for(i=0; i<ROWS; i++){
  127.       for(j=0; j<COLS; j++){
  128.          k = image[i][j];
  129.          histogram[k] = histogram[k] + 1;
  130.       }
  131.    }
  132. }  /* ends calculate_histogram */
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.         /*********************************************
  142.         *
  143.         *       show_histogram(histogram)
  144.         *
  145.         *        This function shows the histogram
  146.         *        on the screen as numbers and stars.
  147.         *
  148.         **********************************************/
  149.  
  150.  
  151.  
  152. show_histogram(histogram)
  153.         unsigned long histogram[];
  154. {
  155.         int     count,
  156.                 i,
  157.                 j;
  158.         unsigned long max, scale;
  159.  
  160.  
  161.         max   = 0;
  162.         count = 0;
  163.  
  164.         for(i=0; i<GRAY_LEVELS; i++)
  165.            if(histogram[i] > max)
  166.               max = histogram[i];
  167.  
  168.         if(max > (70 - 12))
  169.            scale = max/(70 - 12);
  170.         else
  171.            scale = 1;
  172.  
  173.         printf("\n max=%ld scale=%ld",max, scale);
  174.  
  175.         printf("\n\ngray    count");
  176.         printf("\nlevel");
  177.  
  178.         for(i=0; i<256; i++){
  179.            if(histogram[i] == 0)
  180.               ++count;
  181.            else 
  182.               count = 0;
  183.  
  184.            if(count < 2){
  185.               printf("\n %4d: %7ld",i,histogram[i]);
  186.               for(j=0; j<((int)(histogram[i]/scale)); j++){
  187.                  printf("*");
  188.               }         /* ends loop over j             */
  189.            }            /* ends if count < 5            */
  190.         }               /* ends loop over i GRAY_LEVELS */
  191. }       /* ends show_histogram */
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.         /*********************************************
  202.         *
  203.         *        print_histogram(histogram)
  204.         *
  205.         *        This function prints the histogram
  206.         *       input to the function.
  207.         *
  208.         **********************************************/
  209.  
  210.  
  211.  
  212. print_histogram(histogram, name)
  213.         char name[];
  214.         unsigned long histogram[];
  215. {
  216.         char    string[300],
  217.                 output[300];
  218.  
  219.         int     count,
  220.                 i,
  221.                 j,
  222.                 line_counter,
  223.                 print_counter;
  224.         unsigned long scale, max;
  225.  
  226.         FILE *printer;
  227.  
  228.         if( (printer = fopen("prn", "w")) == NULL)
  229.            printf("\nPH> Could not open printer");
  230.         else
  231.            printf("\nPH> The print file is opened");
  232.  
  233.         max           = 0;
  234.         count         = 0;
  235.         print_counter = 0;
  236.  
  237.         for(i=0; i<256; i++)
  238.            if(histogram[i] > max)
  239.               max = histogram[i];
  240.  
  241.         if(max > (PRINT_WIDTH - 12))
  242.            scale = max/(PRINT_WIDTH - 12);
  243.         else
  244.            scale = 1;
  245.  
  246.         printf("\n max=%ld scale=%ld",max, scale);
  247.  
  248.         printf("\nPI> Print header");
  249.         line_counter = 0;
  250.         long_clear_buffer(string);
  251.         sprintf(string, "          This image is -- %s --", name);
  252.         my_fwriteln(printer, string);
  253.         ++line_counter;
  254.  
  255.         long_clear_buffer(string);
  256.         sprintf(string, " ");
  257.         my_fwriteln(printer, string);
  258.         ++line_counter;
  259.  
  260.         long_clear_buffer(string);
  261.         sprintf(string, "          gray    count");
  262.         my_fwriteln(printer, string);
  263.         ++line_counter;
  264.         long_clear_buffer(string);
  265.         sprintf(string, "          level");
  266.         my_fwriteln(printer, string);
  267.         ++line_counter;
  268.  
  269.         for(i=0; i<256; i++){
  270.            if(histogram[i] == 0)
  271.               ++count;
  272.            else 
  273.               count = 0;
  274.  
  275.            if(count < 2){
  276.               printf(" %4d: %7ld",i,histogram[i]);
  277.               print_counter++;
  278.               if(print_counter >= 6){
  279.                  printf("\n");
  280.                  print_counter = 0;
  281.               }  /* ends if print_counter >= 6 */
  282.  
  283.               long_clear_buffer(string);
  284.               sprintf(string, 
  285.                 "           %3d: %7ld ->",i,histogram[i]);
  286.               my_fwrite(printer, string);
  287.               long_clear_buffer(string);
  288.               sprintf(output, " ");
  289.               for(j=0; j<((int)(histogram[i]/scale)); j++){
  290.                  sprintf(string, "*");
  291.                  append_string(string, output);
  292.               }         /* ends loop over j                */
  293.               my_fwriteln(printer, output);
  294.               ++line_counter;
  295.               if(line_counter >= 55){
  296.                  line_counter = 0;
  297.                  putc(FORMFEED, printer);
  298.               }  /* ends if line_counter >=55  */
  299.  
  300.            }                    /* ends if count < 2 */
  301.         }                 /* ends loop over i */
  302.         putc(FORMFEED, printer);
  303.         fclose(printer);
  304.  
  305. }        /* ends print_histogram */
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.         /*********************************************
  315.         *
  316.         *       display_histogram(histogram)
  317.         *
  318.         *       This function shows the histogram
  319.         *       input to the function.
  320.         *
  321.         **********************************************/
  322.  
  323.  
  324.  
  325. display_histogram(histogram, x, y,
  326.                   line_color,data_color)
  327.         int data_color, line_color, x, y;
  328.         unsigned long histogram[];
  329. {
  330.         int     count,
  331.                 i,
  332.                 j,
  333.                 length;
  334.         unsigned long scale, max;
  335.  
  336.         max   = 0;
  337.         count = 0;
  338.  
  339.         for(i=0; i<256; i++)
  340.            if(histogram[i]